Repository navigation
Conversation
Restricting lines to the image in Python (python-pillow#10610) moves their end points, so that Bresenham's algorithm sets different pixels inside the image than for the whole line, and the same line moved by whole pixels no longer gives the same pixels. Instead, keep the end points and skip the steps before and after the image in C. The error term for the first visible step follows from the minor axis offset k(i) = floor((2 * minor * i + major) / (2 * major)), computed with 64-bit integers, as the coordinates are 32-bit and the product of the smaller delta and the number of skipped steps can reach 2**64. The dash position is taken modulo the length of the pattern, so that long dashed lines take no time either. Python now only restricts lines that reach outside the range of C ints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Could you make an argument for why it is necessary that they be the same? #10610 was about trying to prevent long lines from using excessive resources. There is a balance between managing resources and providing precision. If a user wants pixel perfect accuracy, I'm not convinced they should be drawing outside of the image in the first place. Are you personally drawing lines with points outside an image? That would be interesting to hear about. However, if this is merely AI finding an edge case, there are other priorities. |
Merging this PR will degrade performance by 34.04%3 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Follow-up to #10610.
Restricting a line to the image in Python moves its end points. Bresenham's algorithm on the shortened segment does not set the same pixels as on the whole line, so a line that leaves the image is drawn differently from the same line on a larger image that is then cropped. Before #10610, the pixels inside the image were those of the whole line.
I compared 36,000 random lines with integer coordinates (2, 3 and 6 points, solid and with four
dashpatterns, drawn on 40x40 and cropped from 400x400): 21,734 differ on currentmain, none with this change.Changes proposed in this pull request:
line((0, 0, 2**30, 1)), with and withoutdash, takes well under a millisecond.ImageDraw._restrict_linesnow only restricts lines that reach outside the range of C ints, where Liang–Barsky clipping is still needed. Dashed lines of anywidthgo through it as well, sincewidthis ignored withdash.test_line_partly_outside_imagecompares solid and dashed lines that are partly outside the image with the same lines cropped from a larger image, andtest_line_far_outside_image_dashdraws a dashed line that ends at 2**30. The tests added in Clip width 1 lines to the image #10610 pass unchanged.The full test suite passes on macOS (arm64, clang), apart from
test_imagegrab, which needs the screen recording permission. I have not tested Windows or 32-bit builds.This is also needed for #10575: with #10610 alone, its translation invariance tests for lines fail.
🤖 Generated with Claude Code